package com.yj.forum.controller;

import com.yj.forum.common.AppResult;
import com.yj.forum.common.ResultCode;
import com.yj.forum.exception.ApplicationException;
import com.yj.forum.model.Board;
import com.yj.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "版块接口")
@RestController
@Slf4j
@RequestMapping("/board")
public class BoardController {
    // 从配置⽂件中获取主⻚中显⽰的版块个数，默认为9
    @Value("${yj-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService boardService;
    @ApiOperation("获取首页版块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("主⻚中显⽰的版块个数: indexBoardNum = " + indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        return AppResult.success(boards);
    }

    /**
     * 获取版块详情
     * @param id 版块id
     * @return 板块详情
     */
    @ApiOperation("获取版块详情")
    @GetMapping("/getById")
    public AppResult<Board> getBoardInfo(@ApiParam("版块id") @RequestParam("id") Long id) {
        Board board = boardService.selectById(id);
        if(board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_IS_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_IS_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
